package controller

import (
	"github.com/gin-gonic/gin"
	"goproject/src/model"
	"goproject/src/service"
	"net/http"
	"strconv"
)

// 预约单控制层
func OrderController(r *gin.Engine) {
	// 分页查询预约单列表 api/order/getPageList?pageSize=10&page=0
	r.GET(`/api/order/getPageList`, func(context *gin.Context) {
		var pageSize string = context.Query("pageSize") // 从URL中获取参数
		if pageSize == "" {                             // 判断为控
			pageSize = "10"
		}
		var page string = context.Query("page")
		if page == "" { // 判断为控
			page = "0"
		}
		var orders []model.DesOrder = service.GetAllOrdersPage(pageSize, page)
		context.JSON(http.StatusOK, orders)
	})
	// 删除预约单 api/order/delete?id=1718796090192658433
	r.DELETE(`/api/order/delete`, func(context *gin.Context) {
		var id string = context.Query("id") // 从URL中获取参数
		if id == "" {                       // 判断为控
			context.JSON(http.StatusBadRequest, "删除的id不能为空")
			return
		}
		service.DeleteOrderById(id)
		context.String(http.StatusOK, "删除成功")
	})
	// 获取预约单总数 api/order/totals
	r.GET(`/api/order/totals`, func(context *gin.Context) {
		var totols int64 = service.QueryOrderTotals()
		context.String(http.StatusOK, strconv.FormatInt(totols, 10))
	})
	// 获取全量的预约单数据  /api/order/list?orderName=
	r.GET(`/api/order/list`, func(context *gin.Context) {
		var orderName string = context.Query("orderName") // 从URL中获取参数
		var orders []model.DesOrder = service.GetAllOrdersFilter(orderName)
		context.JSON(http.StatusOK, orders)
	})
}
